package com.mall.common.config;

import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.MDC;

import java.text.MessageFormat;

/**
 * 日志追踪ID转换器
 */
public class TraceIdConverter extends MessageConverter {

    private static final String TRACE_ID = "traceId";

    @Override
    public String convert(ILoggingEvent event) {
        String traceId = MDC.get(TRACE_ID);
        if (traceId == null) {
            traceId = "N/A";
        }
        return MessageFormat.format("[TraceId:{0}]", traceId);
    }
}